import request from './request'
import Interface from './interface'
import { Toast } from 'vant'

const api = {
    install: function (Vue) {
        Vue.prototype.$api = this
    }
}

Interface.forEach(v => {
    if (!v.desc) {
        new Error('你的方法没有添加描述信息！')
    }
    api[v.name] = (params, isLoading = true) => {
        if (isLoading) {
            Toast.loading({
                message: '加载中...',
                duration: 0,
                forbidClick: true
            })
        }

        // 过滤空参数
        if (params && JSON.stringify(params) != '{}') {
            let arr = Object.keys(params)
            arr.forEach(item => {
                if (params[item] === null || params[item] === undefined) {
                    delete params[item]
                }
            })
        }

        if (v.method == 'get') {
            return request({
                url: v.path,
                method: v.method,
                params
            })
        } else {
            return request({
                url: v.path,
                method: v.method,
                data: params
            })
        }
    }
})

export default api
